package com.smart.order.service.impl;

import com.smart.order.dto.OrderDto;
import com.smart.order.service.OrderService;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

// 2.2
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    RocketMQTemplate template;

    /**
     * @return
     */
    @Override
    public String createOrder() {
        //  发送事务消息 (半消息)

        OrderDto orderDto = new OrderDto();
        orderDto.setOrderSn("1");
        orderDto.setProductId(1L);
        orderDto.setQuantity(1);
        //
        Message<OrderDto> message = MessageBuilder.withPayload(orderDto).build();


        TransactionSendResult sendResult = template.sendMessageInTransaction("stock-topic", message, orderDto.getProductId());
        SendStatus sendStatus = sendResult.getSendStatus();
        return null;
    }
}
